/**
 *
 */
package com.zhuoyue.elmt.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.eit.eitsite.common.config.Global;
import com.eit.eitsite.common.persistence.Page;
import com.eit.eitsite.common.web.BaseController;
import com.eit.eitsite.common.utils.StringUtils;
import com.zhuoyue.elmt.entity.MaintenanceRecord;
import com.zhuoyue.elmt.service.MaintenanceRecordService;

/**
 * 电梯维保记录Controller
 * @author chenhao
 * @version 2016-09-07
 */
@Controller
@RequestMapping(value = "${adminPath}/elmt/maintenanceRecord")
public class MaintenanceRecordController extends BaseController {

	@Autowired
	private MaintenanceRecordService maintenanceRecordService;
	
	@ModelAttribute
	public MaintenanceRecord get(@RequestParam(required=false) String id) {
		MaintenanceRecord entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = maintenanceRecordService.get(id);
		}
		if (entity == null){
			entity = new MaintenanceRecord();
		}
		return entity;
	}
	
	@RequiresPermissions("elmt:maintenanceRecord:view")
	@RequestMapping(value = {"list", ""})
	public String list(MaintenanceRecord maintenanceRecord, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<MaintenanceRecord> page = maintenanceRecordService.findPage(new Page<MaintenanceRecord>(request, response), maintenanceRecord); 
		model.addAttribute("page", page);
		return "zhuoyue/elmt/maintenanceRecordList";
	}

	@RequiresPermissions("elmt:maintenanceRecord:view")
	@RequestMapping(value = "form")
	public String form(MaintenanceRecord maintenanceRecord, Model model) {
		model.addAttribute("maintenanceRecord", maintenanceRecord);
		return "zhuoyue/elmt/maintenanceRecordForm";
	}

	@RequiresPermissions("elmt:maintenanceRecord:edit")
	@RequestMapping(value = "save")
	public String save(MaintenanceRecord maintenanceRecord, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, maintenanceRecord)){
			return form(maintenanceRecord, model);
		}
		maintenanceRecordService.save(maintenanceRecord);
		addMessage(redirectAttributes, "保存维保记录成功");
		return "redirect:"+Global.getAdminPath()+"/elmt/maintenanceRecord/?repage";
	}
	
	@RequiresPermissions("elmt:maintenanceRecord:edit")
	@RequestMapping(value = "delete")
	public String delete(MaintenanceRecord maintenanceRecord, RedirectAttributes redirectAttributes) {
		maintenanceRecordService.delete(maintenanceRecord);
		addMessage(redirectAttributes, "删除维保记录成功");
		return "redirect:"+Global.getAdminPath()+"/elmt/maintenanceRecord/?repage";
	}

}